package com.example.etwzgzxt.view.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.etwzgzxt.R;
import com.example.etwzgzxt.tool.HttpHelper;
import com.example.etwzgzxt.tool.InfoApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.message.BasicNameValuePair;

public class InsertDeviceActivity extends AppCompatActivity {

    EditText etID, etName, etHoldName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert_device);

        etID = (EditText) findViewById(R.id.etId);
        etName = (EditText) findViewById(R.id.etName);
        etHoldName = (EditText) findViewById(R.id.etHoldName);
        TextView btn = (TextView) findViewById(R.id.btnSaveInfo);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new BackgroundTask().execute();
            }
        });
    }

    public class BackgroundTask extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... params) {
            return updateInfo();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            parseResult(result);
        }
    }

    public String updateInfo() {
        String baseURL = ((InfoApplication)getApplicationContext()).getBasehost()+"/device/insertdevice.do";
        NameValuePair pair[] = new NameValuePair[4];
        pair[0] = new BasicNameValuePair("access_token",  ((InfoApplication) getApplication()).getAccess_token());
        pair[1] = new BasicNameValuePair("devicecode", etID.getText().toString());
        pair[2] = new BasicNameValuePair("devicename", etName.getText().toString());
        pair[3] = new BasicNameValuePair("holder", etHoldName.getText().toString());
        List<NameValuePair> pairList = new ArrayList<>();
        Collections.addAll(pairList, pair);
        for (NameValuePair pairs: pairList) {
            System.out.println(pairs.getName()+" "+pairs.getValue());
        }
        return new HttpHelper().post(baseURL, pairList);
    }

    public void parseResult(String result) {
        if (result == null) {
            Toast.makeText(this, "请检查网络设置", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            JSONObject jsonObject = new JSONObject(result);
            switch (jsonObject.getInt("code")) {
                case 200:
                    finish();
                    break;
                case 403:

                    Toast.makeText(this, "Not Acceptable", Toast.LENGTH_SHORT).show();
                    break;
                case 406:
                    Toast.makeText(this, "Forbidden", Toast.LENGTH_SHORT).show();
                    break;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
